<?php
namespace Admin\Controller;
class WorkflowNodeController extends BaseController {
	public function index(){
		//echo '<br>--www--$workflowid'.$workflowid;
	    $workflowid=I('get.workflowid');
	    if(I('post.workflowid')){
	        $workflowid=I('post.workflowid');
	    }
	    if($workflowid){
            $object=M('Workflownode');
            import('ORG.Util.Page');			//Page类的引入
            $count=$object->where($where)->count(); //获取符合条件的数据总数count
    		$Page=new \Org\Util\Page($count, 10);			//实例化page类，传入数据总数和每页显示10条内容
    		$show=$Page->show();
    		$list=$object->where('workflowid='.$workflowid)->limit($Page->firstRow.','.$Page->listRows)->select();
    		
    		$list=$object->where('bs_Workflownode.workflowid='.$workflowid)
    		->field('bs_Workflownode.*,b.name as wfname,c.name as nxname,d.nickname as uname,e.name as dname')
    		->join('LEFT JOIN bs_Workflow b       ON bs_Workflownode.workflowid = b.id')
    		->join('LEFT JOIN bs_Workflownode c   ON bs_Workflownode.nextnodeid = c.id')
    		->join('LEFT JOIN bs_User d           ON bs_Workflownode.handlerobject1 = d.id')
    		->join('LEFT JOIN bs_Department e           ON bs_Workflownode.handlerobject2 = e.id')
    		->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->select();
    		
            $this->assign('list',$list);
            $this->assign("page",$show);
            $this->display();
	    }
	}
    public function editUI(){
        $workflowid = I('get.workflowid');
        $id = I('get.id');
        if($workflowid){
            $where = " workflowid=".$workflowid;
    		if(I('get.id')){
    			$this->assign('object',M('Workflownode')->where('id='.I('get.id'))->find());
    			$where=$where." and id<>".$id;
    		}
            $this->assign('workflowlist',M('Workflow')->where()->select());
            $this->assign('userlist',M('User')->where()->select());
            $this->assign('deplist',M('Department')->where()->select());
            $this->assign('nodelist',M('Workflownode')->where($where)->select());
           // echo  M("Workflownode")->getLastSql();
            $this->display('editUI');
        }
    }
    public function setFieldUI(){
        $workflowid = I('get.workflowid');
        $id = I('get.id');
        if($workflowid){
            echo '<br>=========='.$workflowid;
            //根据工作流id找到工作流对象
            $Object_wf = M('Workflow')->where('id='.$workflowid)->find();
    		if($id){
            echo '<br>=========='.$id;
                //根据表单id获得表单字段信息
    			$this->assign('fieldlist',M('Formfield')->where('formid='.$Object_wf[formid])->select());
    			$this->assign('nodefieldlist',M('Workflownodefield')->where('nodeid='.$id)->select());
    		}
            //echo  M("Workflownode")->getLastSql();
            $this->display();
        }
    }
    public function setFieldOperation(){
        $workflowid = I('post.workflowid');
        $id = I('post.id');
        echo '===================================='.$id;
        if($id){
        echo '===================================='.$id;
            $fieldidArray = I('post.fieldid');
            $readwriteArray = I('post.readwrite');
			$Object1 = M("Workflownodefield");
			//删掉所有
			$Object1->where('nodeid='.$id)->delete();
			for ($i=0;$i<count($fieldidArray);$i++){
			    //逐个添加
				$Object1 -> nodeid = $id;
				$Object1 -> formfieldid = $fieldidArray[$i];
				$Object1 -> readwrite = $readwriteArray[$i];
				echo '<br>'.$Object1->nodeid."===".$Object1->formfieldid."===".$Object1->readwrite."===";
				$Object1->add();
			}
			echo '<br>----$workflowid'.$workflowid;
			$this->success('修改成功',U('index').'?workflowid='.$workflowid);
        }
    }
    public function editOperation(){
		$Object = M("Workflownode");   
		$Object->create();
		$id = I('post.id');
		$workflowid = I('post.workflowid');
		if($id){
			if($Object->save()){
				$this->success('修改成功',U('index').'?workflowid='.$workflowid);
			}else{
				dump($Object->_sql());
				$this->error('修改失败');
			}
		}else{
			if($Object->add()){
				$this->success('添加成功',U('index').'?workflowid='.$workflowid);
			}else{
				dump($Object->_sql());
				$this->error('添加失败');
			}
		}
    }
    public function removeOperation(){
		$Object = M("Workflownode");
		if($Object->delete(I('get.id'))){
			$this->success('删除成功',U('index'));
		}else{
			dump($Object->_sql());
			$this->error('删除失败');
		}
    }
}